/* 213. 打家劫舍 II 首尾是相连的 */
/**
 * @param {number[]} nums
 * @return {number}
 */
var rob = function (nums) {
    const len = nums.length
    if (len < 3) return Math.max(...nums)

    /* 首位相连,其实就是把首去掉或者把尾去掉放到第一个里面去 */
    return Math.max(helper(nums, 0, len - 2), helper(nums, 1, len - 1))
};

/* 滚动数组 */
const helper = (nums, start, end) => {
    let first = nums[start], second = Math.max(nums[start], nums[start + 1])
    for (let i = start + 2; i <= end; i++) {
        const temp = second
        second = Math.max(first + nums[i], second)
        first = temp
    }

    return second
}
const result = rob([1, 2, 3, 1])
console.log('result :>>', result);